package com.gitee.wsl.doc.prosemirror.model.fragment.ext

import com.gitee.wsl.doc.prosemirror.model.fragment.Fragment
import com.gitee.wsl.doc.prosemirror.model.node.Node


fun mapFragment(fragment: Fragment, f: (child: Node, parent: Node, i: Int) -> Node, parent: Node): Fragment {
    val mapped = mutableListOf<Node>()
    for (i in 0 until fragment.childCount) {
        var child = fragment.child(i)
        if (child.content.size != 0) child = child.copy(mapFragment(child.content, f, child))
        if (child.isInline) child = f(child, parent, i)
        mapped.add(child)
    }
    return Fragment.fromArray(mapped)
}

fun Fragment.map(parent: Node,f: (child: Node, parent: Node, i: Int) -> Node): Fragment = mapFragment(this,f,parent)